<?php


namespace Drupal\shushengyuan\Schemas;

use Drupal\s_commerce_service\Entity\SCommerceService;
use EntityListWrapper;
use Exception;

/**
 * Class OrderServiceInfo
 *
 * @package Drupal\youjigonglue\Schemas
 * @OA\Schema(
 *   schema="OrderServiceInfo",
 *   type="object",
 *   description="售后单详情",
 *   allOf={
 *       @OA\Schema(ref="#/components/schemas/OrderServiceItem"),
 *   }
 * )
 */
class OrderServiceInfo extends OrderServiceItem {

  protected function setValue() {
    parent::setValue();
    if ($this->entity instanceof SCommerceService) {

      $field_images = $this->wrapper->field_images;
      $imgs = [];
      $imgs_original = [];
      if ($field_images instanceof EntityListWrapper) {
        foreach ($field_images->getIterator() as $item) {
          $imgs[] = $this->imageStyleUrl('OrderServiceInfoimgs', $item->value());
          $imgs_original[] = $this->imageStyleUrl('OrderServiceInfoimgs_original', $item->value());
        }
      }

      $field_shipping_images = $this->wrapper->field_shipping_images;
      $shipping_imgs = [];
      $shipping_imgs_original = [];
      if ($field_images instanceof EntityListWrapper) {
        foreach ($field_images->getIterator() as $item) {
          $shipping_imgs[] = $this->imageStyleUrl('OrderServiceInfoshipping_imgs', $item->value());
          $shipping_imgs_original[] = $this->imageStyleUrl('OrderServiceInfoshipping_imgs_original', $item->value());
        }
      }

      $reason = '';
      try {
        $reason = $this->wrapper->reason->name->value();
      } catch (Exception $exception) {
      }

      $value = [
        /**
         * @OA\Property(
         *   property="reason",
         *   type="string",
         *   description="退款原因",
         * ),
         */
        'reason' => $reason,
        /**
         * @OA\Property(
         *   property="created",
         *   type="string",
         *   description="申请时间",
         * ),
         */
        'created' => $this->formatDate($this->entity->created, 'Y-m-d H:i:s'),
        /**
         * @OA\Property(
         *   property="imgs",
         *   type="array",
         *   description="退款凭证",
         *   @OA\Items(type="string")
         * ),
         */
        'imgs' => $imgs,
        /**
         * @OA\Property(
         *   property="imgsOriginal",
         *   type="array",
         *   description="退款凭证原图",
         *   @OA\Items(type="string")
         * ),
         */
        'imgsOriginal' => $imgs_original,
        /**
         * @OA\Property(
         *   property="shippingImgs",
         *   type="array",
         *   description="退货凭证",
         *   @OA\Items(type="string")
         * ),
         */
        'shippingImgs' => $shipping_imgs,
        /**
         * @OA\Property(
         *   property="shippingImgsOriginal",
         *   type="array",
         *   description="退货凭证原图",
         *   @OA\Items(type="string")
         * ),
         */
        'shippingImgsOriginal' => $shipping_imgs_original,
        'head_info' => '待处理',
      ];
      $this->merge($value);
    }
  }

}
